//
//  SweepCodeViewController.m
//  Express
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "SweepCodeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "BaseHeader.h"

@interface SweepCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic, strong) AVCaptureSession *sessiom;

@end

@implementation SweepCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *sweepIV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    sweepIV.image = [UIImage imageNamed:@"sweepBg"];
    [self.view addSubview:sweepIV];
    
    UIButton *backBuutton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    backBuutton.frame = CGRectMake(kWidth - 70, 50, 70, 30);
    [backBuutton setTitle:@"返回" forState:(UIControlStateNormal)];
    [backBuutton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [backBuutton addTarget:self action:@selector(turnBack) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backBuutton];
    
    NSError *error;
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    self.sessiom = [[AVCaptureSession alloc] init];
    [self.sessiom setSessionPreset:AVCaptureSessionPresetHigh];
    [self.sessiom addInput:input];
    [self.sessiom addOutput:output];
    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code];
    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.sessiom];
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    layer.frame = self.view.layer.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];
    [self.sessiom startRunning];
    
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    if (metadataObjects.count > 0) {
        AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
        if (metadataObject.stringValue != nil) {
            [self.sessiom stopRunning];
            [self dismissViewControllerAnimated:YES completion:^{
                [self.delegate returnWithSweepCode:metadataObject.stringValue];
                NSLog(@"sweep success");
            }];
        }
    }
    
}

- (void)turnBack {
    NSLog(@"back");
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
